﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;
using System.Data.Common;
using System.Data;

namespace Wjtour.BLL
{
    public class adminlog_bll : base_dll
    {
        public PaginationData<Wjtour.Domain.adminlog> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and nick like '%" + Utility.GetSafeSql(q) + "%' ";
            string sql = string.Format(@"select * from adminlog 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.adminlog> data = GetPaginationData<Wjtour.Domain.adminlog>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.adminlog Load(int id)
        {
            string sql = "select * from adminlog where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.adminlog>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.adminlog entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE [adminlog] SET 
                [admin_name] = @admin_name,
                [ip] = @ip,
                [mod] = @mod,
                [done] = @done,
                [addtime] = @addtime
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);
            }
            else
            {
                sql = @"INSERT INTO [adminlog]( 
                [admin_name],[ip],[mod],[done],[addtime])VALUES(
                @admin_name,@ip,@mod,@done,@addtime)";
            }
            DbAccess.AddInParameter(cmd, "@admin_name", DbType.String, entity.admin_name);
            DbAccess.AddInParameter(cmd, "@ip", DbType.String, entity.ip);
            DbAccess.AddInParameter(cmd, "@mod", DbType.String, entity.mod);
            DbAccess.AddInParameter(cmd, "@done", DbType.String, entity.done);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;
        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from adminlog where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
